package com.xzz.window;

import java.util.HashMap;

/**
 * @author: hhz
 * @create: 2021-12-24 15:12
 * 3. 无重复字符的最长子串
 * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。
 **/
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }
    public static int lengthOfLongestSubstring(String s) {
       HashMap<Character,Integer> window = new HashMap<>();
       int left = 0;
       int right = 0;
       int len = 0;
       while (right<s.length()){
           char c = s.charAt(right);
           right++;
           window.put(c,window.getOrDefault(c,0)+1);

           while (window.get(c)>1){
               char d = s.charAt(left);
               left++;

               window.put(d,window.getOrDefault(d,0)-1);
           }
           len = Math.max(right-left,len);
       }
       return len;
    }
}
